package com.lzq.badminton.controller;

import com.lzq.badminton.dto.ArenaDto;
import com.lzq.badminton.dto.UpdateArenaDto;
import com.lzq.badminton.entity.Arena;
import com.lzq.badminton.enums.AppHttpCodeEnum;
import com.lzq.badminton.service.ArenaService;
import com.lzq.badminton.utils.BeanCopyUtils;
import com.lzq.badminton.vo.ArenaVo;
import com.lzq.badminton.vo.ResponseResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController()
@RequestMapping("/arena")
public class ArenaController {
    @Autowired
    private ArenaService arenaService;
    @PostMapping("/create")
    @ApiOperation(value="创建球馆")
    public ResponseResult createArena(@RequestBody ArenaDto arenaDto){
        return arenaService.addArena(arenaDto);
    }

    @GetMapping("/list")
    @ApiOperation(value="查询球馆们")
    public ResponseResult listArena(){
        List<ArenaVo> arenaVoList = arenaService.listArenaAdmin();
        return ResponseResult.okResult(arenaVoList);
    }

    @GetMapping("/state/{id}/{state}")
    public ResponseResult stateArena(@PathVariable Long id, @PathVariable Integer state){
        if(arenaService.updateState(id, state)){
            return ResponseResult.okResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.ORENA_STATE);
    }

    @GetMapping("/delete/{id}")
    public ResponseResult deleteArena(@PathVariable Long id){
        if(arenaService.removeById(id)){
            return ResponseResult.okResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.ARENA_DELETE);
    }

    @GetMapping("/deteil/{id}")
    public ResponseResult deteilArena(@PathVariable Long id){
        ArenaVo arenaVo = BeanCopyUtils.copyBean(arenaService.getById(id), ArenaVo.class);
        return ResponseResult.okResult(arenaVo);
    }

    @PostMapping("/edit")
    public ResponseResult editArena(@RequestBody UpdateArenaDto updateArenaDto){
        Arena arena = BeanCopyUtils.copyBean(updateArenaDto, Arena.class);
        if(arenaService.updateById(arena)){
            return ResponseResult.okResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.ARENA_EDIT);
    }


}
